package com.dengtacj.puppeteerspider.config;

import com.ruiyun.jvppeteer.core.Puppeteer;
import com.ruiyun.jvppeteer.core.browser.Browser;
import com.ruiyun.jvppeteer.core.browser.BrowserFetcher;
import com.ruiyun.jvppeteer.options.LaunchOptions;
import com.ruiyun.jvppeteer.options.LaunchOptionsBuilder;
import com.ruiyun.jvppeteer.options.Viewport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.function.BiConsumer;

public class PuppeteerConfig {

    String proxyUrl=null;
    Logger logger;

    public PuppeteerConfig() {
        this.proxyUrl = null;
        logger = LoggerFactory.getLogger(PuppeteerConfig.class);
    }
    public PuppeteerConfig(String proxyUrl) {
        this.proxyUrl = proxyUrl;
        logger = LoggerFactory.getLogger(PuppeteerConfig.class);
    }

    public  LaunchOptions config(){
        ArrayList<String> arrayList = new ArrayList<>();
        arrayList.add("--no-sandbox");
        arrayList.add("--disable-setuid-sandbox");
        arrayList.add("--disable-gpu");
        arrayList.add("--disable-dev-shm-usage");
        arrayList.add("--disable-extensions");
        arrayList.add("--no-first-run");
        arrayList.add("--no-first-run");
        arrayList.add("--disable-infobars");
        arrayList.add("--ignore-certificate-error");
        arrayList.add("--ignore-ssl-errors");

        if(proxyUrl!=null){
            arrayList.add("--proxy-server=http://"+proxyUrl);
        }

        LaunchOptions launchOptions = new LaunchOptionsBuilder().withArgs(arrayList).withHeadless(false).build();
        launchOptions.setTimeout(40*1000);
        // true 为静默执行 ，false为 开启浏览器执行
        launchOptions.setHeadless(true);
        launchOptions.setDevtools(false);
        launchOptions.setDumpio(true);
        Viewport viewport = new Viewport();
        viewport.setHeight(1040);
        viewport.setWidth(1920);
        viewport.setIsLandscape(false);
        launchOptions.setViewport(viewport);
        //启动
        List<String> sss = new ArrayList<>();
        sss.add("--enable-automation");
        launchOptions.setIgnoreDefaultArgs(sss);
        return  launchOptions;
    }

    public Browser initBrowser() {
        Puppeteer puppeteer = new Puppeteer();
        BrowserFetcher browserFetcher = puppeteer.createBrowserFetcher();
        BiConsumer<Integer,Integer> processCallback = (a, b) -> {
             logger.info("下载进度："+ a+":"+b);
        };

        try {
            browserFetcher.download("722234",processCallback);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }

        LaunchOptions launchOptions = config();
        Browser launch = null;
        try {
             launch = puppeteer.launch(launchOptions);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return launch;
    }

}
